package org.ragdollcat.secondaiagent.third.baidu.transfer;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

public class TransApi {

    private final String appid = "20251002002468251";

    private final String securityKey = "63b2ahuiE0WKMC27Kujc";

    /**
     *
     * @param query 要翻译的文本
     * @param from  源语言
     * @param to    目标语言
     * @return
     */
    public BaiduTranslateResponse getTransResult(String query, String from, String to) {
        Map<String, String> params = buildParams(query, from, to);
        String result = HttpGet.get("https://fanyi-api.baidu.com/api/trans/vip/translate", params);
        return JSONUtil.toBean(result, BaiduTranslateResponse.class);
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);

        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名
        String src = appid + query + salt + securityKey; // 加密前的原文
        params.put("sign", MD5.md5(src));

        return params;
    }




}
